﻿/*
 *
 * DX_SourceOutliner
 * Copyright (C)2009
 * http://code.google.com/p/dxcsourceoutliner
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Collections;
using DevExpress.CodeRush.Core;

namespace DX_SourceOutliner
{
    public class NodeFilter
    {
        private bool _sortAlphabetically = false;

        public bool SortAlphabetically
        {
            get { return _sortAlphabetically; }
            set { _sortAlphabetically = value; }
        }


        private bool BeginsWithUppercaseLetter(string candidate)
        {
            return (new Regex("^[A-Z]")).IsMatch(candidate);
        }

        //private bool IsAllUppercase(string candidate)
        //{
        //    return (candidate == candidate.ToUpper());
        //}

        //private List<TreeNode> new_GetMatchingNodes_using_CamelCaseFilter(IEnumerable treeNodes, string filterPattern)
        //{
        //    List<TreeNode> matchingNodes = new List<TreeNode>();
        //    CamelCaseFilter filter;

        //    if (filterPattern.Trim() != string.Empty && BeginsWithUppercaseLetter(filterPattern))
        //    {
        //        filter = new CamelCaseFilter(filterPattern);
        //        
        //        var initialNodes = from TreeNode node in treeNodes
        //                           where filter.Accept(node.Text, IsAllUppercase(filterPattern))
        //                           select node;

        //        if (_sortAlphabetically)
        //            initialNodes = initialNodes.OrderBy(node => node.Text);

        //        matchingNodes = initialNodes.ToList();
        //    }
        //    else
        //    {
        //        var initialNodes = from TreeNode x in treeNodes where x.Text.ToLower().Contains(filterPattern.ToLower()) select x;

        //        if (_sortAlphabetically)
        //            initialNodes = initialNodes.OrderBy(node => node.Text);

        //        matchingNodes = initialNodes.ToList();
        //    }

        //    return matchingNodes;
        //}



        public List<TreeNode> GetMatchingNodes(IEnumerable treeNodes, string filterPattern)
        {
            string matchingPattern = "^";
            string negationPattern = "[^A-Z ]*";

            List<TreeNode> matchingNodes = new List<TreeNode>();

            if (filterPattern.Trim() != string.Empty && BeginsWithUppercaseLetter(filterPattern))
            {
                foreach (char pascalIdentifier in filterPattern.ToCharArray())
                {
                    matchingPattern += pascalIdentifier.ToString() + negationPattern;
                }

                try
                {
                    Regex regexEngine = new Regex(matchingPattern);

                    var initialNodes = from TreeNode node in treeNodes where regexEngine.IsMatch(node.Text) select node;

                    if (_sortAlphabetically)
                        initialNodes = initialNodes.OrderBy(node => node.Text);

                    matchingNodes = initialNodes.ToList();
                }
                catch (Exception)
                {
                    foreach (TreeNode node in treeNodes)
                    {
                        matchingNodes.Add(node);
                    }
                }

            }
            else
            {
                var initialNodes = from TreeNode x in treeNodes where x.Text.ToLower().Contains(filterPattern.ToLower()) select x;

                if (_sortAlphabetically)
                    initialNodes = initialNodes.OrderBy(node => node.Text);

                matchingNodes = initialNodes.ToList();
            }
            return matchingNodes;
        }

    }
}
